/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bitzer.google;

import com.google.gdata.data.DateTime;
import com.google.gdata.data.extensions.City;
import com.google.gdata.data.extensions.Country;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.Organization;
import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.data.extensions.PostCode;
import com.google.gdata.data.extensions.PostalAddress;
import com.google.gdata.data.extensions.Street;
import com.google.gdata.data.extensions.StructuredPostalAddress;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author ali
 */
public class Utils {


    public static String dataRowElement(String primaryKeyName, String primaryKeyValue) {

        return "<tr data-bitzer-pk-name=\"" + primaryKeyName + "\" data-bitzer-pk-value=\"" + primaryKeyValue + "\">";
    }

    public static String dataCell(String value) {
        return "<td>" + value + "</td>";
    }

    public static String imageDataCell(String value) {
        return "<td><IMG SRC='" + value + "'></td>";
    }

    public static int stringToInt(String intString, int defaultValue) {

        int returnValue = defaultValue;

        try {
            returnValue = (new Integer(intString)).intValue();
        } catch (Exception e) {
            
        }

        return returnValue;

    }


    public static String formattedDateTime(DateTime datetime) {

        DateFormat informat = new SimpleDateFormat(Constants.RFC_822_FORMAT);
        DateFormat outformat = new SimpleDateFormat(Constants.BITZER_DATE_FORMAT);

        try
        {
            Date date = informat.parse(datetime.toStringRfc822());
            return outformat.format(date);

        } catch (ParseException e)
        {
            return "";
        }
    }

    public static DateTime getDateTimeFromString(String bitzerDateTime){
        
       DateFormat informat = new SimpleDateFormat(Constants.BITZER_DATE_FORMAT);

        try
        {
            Date date = informat.parse((URLDecoder.decode((String)bitzerDateTime)));
            return new DateTime(date.getTime());
        } catch (Exception e)
        {
            return null;
        }
    }
    
    
    public static String getEmail(List<Email> emails, String rel) {

        for (Email email : emails) {
            if (email != null && rel.equals(email.getRel()))
                return email.getAddress();
        }

        return "";
    }

    public static String getPhone(List<PhoneNumber> phoneNumbers, String rel) {

        for (PhoneNumber phoneNumber : phoneNumbers) {
            if (phoneNumber != null && rel.equals(phoneNumber.getRel()))
                return phoneNumber.getPhoneNumber();
        }

        return "";
    }

    public static Organization getOrganization(List<Organization> organizations) {

        for (Organization organization : organizations) {
            if (organization != null && organization.hasOrgName())
                return organization;
        }

        return null;
    }


    public static StructuredPostalAddress getAddress(List<StructuredPostalAddress> addresses, String rel) {

        for (StructuredPostalAddress address: addresses) {
   
                return address;
        }

        return null;

    }


    public static ArrayList getParams(String queryString, String paramName)
    {
        ArrayList list = new ArrayList();
        if(queryString != null){
            String[] paramArray = queryString.split("&");
            for(int i=0;i<paramArray.length;i++){
                String[] param = paramArray[i].split("=");
                if(param[0].equals(paramName)){
                    list.add(param[1]);
                }
            }
        }
        return list;
    }

   

}
